/*jslint browser: true, devel: true, bitwise: true, white: true, plusplus: true, nomen:true */
/*global $:true, define:true */

define(['WID/Test/Reporter'], function(Reporter) {
"use strict";
return {
	name:'ReporterCase', 
	desc:'Its testing reporter from Test Module',
	
	each: {
		reporter: null
	},
	
	beforeEach: function() {
		this.each.reporter = new Reporter(function(r){});
	},
	// TODO create tests for expections like testDone before testStarted
	tests:[
		function creating() {
			var r = this.each.reporter,report = r.Report;
			
			this.expect(r.CurrentParent).toBe(r.TopResults);
			this.expect(r.CurrentParent).toBeUndefined();
			
			this.expect(r.Failures).toBeType(Array);
			
			this.expect(r.StartTime).toBe(0);
			
			this.expect(report.totalSuites).toBe(0);
			this.expect(report.totalCases).toBe(0);
			this.expect(report.totalTests).toBe(0);
			
			this.expect(r.FullName).toBeType(Array);
			this.expect(r.NameLevel).toBe(0);
		},
		
		function reportStarted() {
			var r = this.each.reporter;
			r.reportStarted();
			
			this.expect(r.CurrentParent).toBe(r.TopResults);
			this.expect(r.CurrentParent.results.start).toBeType(Object);
			this.expect(r.CurrentParent.results.type).toBe(Reporter.EMPTY);
			this.expect(r.CurrentParent.parent).toBeUndefined();
			
			this.expect(r.StartTime).toBeGreaterThan(0);
		},
		
		function reportDone() {
			var r = this.each.reporter;
			r.reportStarted();
			r.reportDone();
			this.expect(r.CurrentParent).toBe(r.TopResults);
		},
		
		function suiteStarted() {
			var r = this.each.reporter, startResult = {name:'test'};
			r.reportStarted();
			r.suiteStarted(startResult);
			
			this.expect(r.CurrentParent).not.toBe(r.TopResults);
			this.expect(r.TopResults.children[0]).toBe(r.CurrentParent);
			this.expect(r.CurrentParent.results.type).toBe(Reporter.SUITE);
			this.expect(r.CurrentParent.results.start).toBe(startResult);
			this.expect(r.NameLevel).toBe(1);
			this.expect(r.FullName.length).toBe(1);
			this.expect(r.FullName[0]).toBe('test');
			this.expect(r.Report.totalSuites).toBe(1);
		},
		
		function suiteDone() {
			var r = this.each.reporter, curr;
			r.reportStarted();
			r.suiteStarted({name:'test'});
			curr = r.CurrentParent;
			r.suiteDone();
		
			this.expect(r.CurrentParent).toBe(r.TopResults);
			this.expect(curr.results.end).toBeDefined();
			this.expect(curr.results.end.failed).toBeFalse();
			this.expect(r.NameLevel).toBe(0);
			
		},
		
		function caseStarted() {
			var r = this.each.reporter, suite,startResult = {name:'test'};
			r.reportStarted();
			r.suiteStarted({name:'suite'});
			suite = r.CurrentParent;
			r.caseStarted(startResult);
			
			this.expect(r.CurrentParent).not.toBe(r.TopResults);
			this.expect(suite.children[0]).toBe(r.CurrentParent);
			this.expect(r.CurrentParent.results.type).toBe(Reporter.CASE);
			this.expect(r.CurrentParent.results.start).toBe(startResult);
			this.expect(r.NameLevel).toBe(2);
			this.expect(r.FullName.length).toBe(2);
			this.expect(r.FullName[1]).toBe('test');
			this.expect(r.Report.totalCases).toBe(1);	
		},
		
		function caseDone() {
			var r = this.each.reporter, suite,curr;
			r.reportStarted();
			r.suiteStarted({name:'test'});
			suite = r.CurrentParent;
			r.caseStarted({name:'test'});
			curr = r.CurrentParent;
			r.caseDone();
		
			this.expect(r.CurrentParent).toBe(suite);
			this.expect(curr.results.end).toBeDefined();
			this.expect(curr.results.end.failed).toBeFalse();
			this.expect(r.NameLevel).toBe(1);
		},
		
		function testStarted(result) {
			var r = this.each.reporter,testStart = {name:'test'};
			r.reportStarted();
			r.suiteStarted({name:'suite'});
			r.caseStarted({name:'case'});
			r.testStarted(testStart);
			
			this.expect(r.CurrentParent).not.toBe(r.TopResults);
			
			this.expect(r.CurrentParent.results.type).toBe(Reporter.TEST);
			this.expect(r.CurrentParent.results.start).toBe(testStart);
			this.expect(r.NameLevel).toBe(3);
			this.expect(r.FullName.length).toBe(3);
			this.expect(r.Report.totalTests).toBe(1);
		},
	
		function testDone(result) {
			var r = this.each.reporter,caseRef,testRef,testResult = {desc:'testDesc'};
			
			r.reportStarted();
			r.suiteStarted({name:'suite'});
			r.caseStarted({name:'case'});
			caseRef = r.CurrentParent;
			r.testStarted({name:'test'});
			testRef = r.CurrentParent;
			r.testDone(testResult);
			
			this.expect(r.CurrentParent).toBe(caseRef);
			this.expect(testRef.results.end).toBe(testResult);
			this.expect(r.NameLevel).toBe(2);
		},
		
		function testDoneFailed(result) {
			var r = this.each.reporter,caseRef,testRef,testResult = {desc:'testDesc',failed:true,exps:['test1']};
			
			r.reportStarted();
			r.suiteStarted({name:'suite'});
			r.caseStarted({name:'case'});
			caseRef = r.CurrentParent;
			r.testStarted({name:'test'});
			testRef = r.CurrentParent;
			r.testDone(testResult);
			
			this.expect(r.CurrentParent).toBe(caseRef);
			this.expect(testRef.results.end).toBe(testResult);
			this.expect(r.NameLevel).toBe(2);
			this.expect(r.CurrentParent.results.end.failed).toBeTrue();
			this.expect(r.Failures[0]).toBe(testRef.results);
		},
		
		function setFailed() {
			var r = this.each.reporter,testRef,caseRef,suiteRef;
			r.reportStarted();
			r.suiteStarted({name:'suite'});
			suiteRef = r.CurrentParent;
			r.caseStarted({name:'case'});
			caseRef = r.CurrentParent;
			r.testStarted({name:'test'});
			testRef = r.CurrentParent;
			r.setFailed(testRef);
			
			this.expect(r.CurrentParent).toBe(testRef);
			this.expect(testRef.results.end.failed).toBeTrue();
			this.expect(caseRef.results.end.failed).toBeTrue();
			this.expect(suiteRef.results.end.failed).toBeTrue();
		}
	]
};
});
